package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.bo.replyBO;
import com.cskaoyan.mall.model.sql.Result;
import com.cskaoyan.mall.model.sql.Type;
import com.cskaoyan.mall.model.bo.GoodsBO;
import com.cskaoyan.mall.model.vo.GoodsInfoVO;
import com.cskaoyan.mall.model.vo.TypeGoodsVO;
import com.cskaoyan.mall.model.vo.noReplyMsgVO;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.implement.GoodServiceImpl;
import com.cskaoyan.mall.utils.FileUploadUtils;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodServiceImpl();

    Gson gson =new Gson();

    /***********************************************    doPost   *******************************************************/
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if("imgUpload".equals(action)){
            imgUpload(request, response);
        }else if ("addGoods".equals(action)){
            addGoods(request,response);
        }else if ("addType".equals(action)){
            addType(request,response);
        }else if ("reply".equals(action)){
            reply(request,response);
        }
    }

    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        replyBO replybo =gson.fromJson(requestBody, replyBO.class);
        goodsService.reply(replybo);
        response.getWriter().println(gson.toJson(Result.ok()));
    }


    /**
     * 添加类目 请求体 ： name="dragon"
     * @param request
     * @param response
     */
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Type type = gson.fromJson(requestBody, Type.class);
        goodsService.addType(type);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 保存商品的业务逻辑
     * 1.获取请求体中的参数
     * 2.处理业务逻辑
     * 3.做出响应
     * @param request
     * @param response
     */
    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsBO goodsBO = gson.fromJson(requestBody, GoodsBO.class);
        goodsService.addGoods(goodsBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 新增商品图片的业务逻辑
     * commons-fileUpload
     * 1.执行具体的业务逻辑，上传图片
     * 2.响应 ----- 抓服务器上的响应报文
     *
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> map = FileUploadUtils.parseRequest(request);
        String file = (String) map.get("file");
        //服务器上面的路径没有携带域名端口号，那么就表示图片就从当前域名端口去取 -----自动拼接域名localhost:8085
        //如果你的图片和页面不在一个域内，那么应当指明你文件所在的域
        //直接返回file ，是从localhost:8085取得，而我们的图片在哪？？ ---> localhost:8084 ,但是生产环境下会改变，应该用配置文件装起来
        String domain = (String) getServletContext().getAttribute("domain");
        response.getWriter().println(gson.toJson(Result.ok(domain + file)));

    }


    /***********************************************    doGet   *******************************************************/
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String requestURI = request.getRequestURI();
            String action = requestURI.replace("/api/admin/goods/", "");
            if("getType".equals(action)){
                getType(request, response);
            }else if("getGoodsByType".equals(action)){
                getGoodsByType(request,response);
            }else if ("getGoodsInfo".equals(action)){
                getGoodsInfo(request,response);
            }else if("deleteGoods".equals(action)){
                deleteGoods(request,response);
            }else if ("deleteType".equals(action)){
                deleteType(request,response);
            }else if ("noReplyMsg".equals(action)){
                noReplyMsg(request,response);
            }else if ("repliedMsg".equals(action)){
                repliedMsg(request,response);
            }
}

    /**
     * 与noReplyMsg一样道理
     * @param request
     * @param response
     */
    private void repliedMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<noReplyMsgVO> noReplyMsgVOS = goodsService.ReplyMsg();
        response.getWriter().println(gson.toJson(Result.ok(noReplyMsgVOS)));
    }

    /**
     * 获取未回复的留言信息list
     * @param request
     * @param response
     */
    private void noReplyMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<noReplyMsgVO> noReplyMsgVOS = goodsService.noReplyMsg();
        response.getWriter().println(gson.toJson(Result.ok(noReplyMsgVOS)));
    }

    /**
     * 删除类目 typeId=5
     * @param request
     * @param response
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("typeId");
        goodsService.deleteType(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 删除商品：参数：----> id=484 面向goods 表
     * @param request
     * @param response
     */
    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        goodsService.deleteGoods(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 编辑，思路是获取id  ------ 此id 是数据库表中goods表的id
     * 数据库依据此id进行查询，然后返回数据结构是：
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GoodsInfoVO goodsInfoVOList = goodsService.getGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsInfoVOList)));
    }

    //Request URL: http://localhost:8084/api/admin/goods/getType

    /**
     * 获取某个分类下的商品信息
     * 1.获取分类id     ------typeId=1  -----------getParameter
     * 2.数据库查询
     * 3.按照一定的数据结构返回
     *
     * Request URL: http://localhost:8084/api/admin/goods/getGoodsByType?typeId=1
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<TypeGoodsVO> goodsVOS = goodsService.getGoodsType(typeId);
        response.getWriter().println(gson.toJson(Result.ok(goodsVOS)));
    }

    /**
     * 加载商品的所有分类
     * 1.没有请求参数
     *
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        response.getWriter().println(gson.toJson(Result.ok(typeList)));
    }
}
